<template>
  <div class="page">
    <div class="smallLogo"><img src="../../assets/front-img/logo.png" alt=""></div>
    <div class="title1">2022众茶国际（云南）茶叶网络交易会</div>
    <div class="title2">暨茶文化传播大使选拔赛答题</div>
    <div class="wrapper">
      <div class="content">
        <div class="title">
          <div>{{ currentModel.title }}</div>
          <el-radio-group class="option" v-model="currentModel.radio">
            <el-radio v-for="(option) in currentModel.optionArray" :key="option + currentModel.title" :label="option.substring(0,1)">{{ option }}</el-radio>
          </el-radio-group>
        </div>
      </div>
    </div>
    <div class="buttonBg">
      <div class="info">{{ currentPage }}</div>
      <el-button v-if="currentIndex !== 0" class="leftBtn" type="primary" @click='changeBtn(0)'>上一题</el-button>
      <el-button v-if="currentIndex !== (questionList.length - 1)" class="rightBtn" type="primary" @click='changeBtn(1)'>下一题</el-button>
      <el-button v-if="currentIndex === (questionList.length - 1)" class="rightBtn" type="success" @click='submitAnswer'>提交答案</el-button>
    </div>
  </div>
</template>

<script>
import { getPaper, saveAnswer } from '@/api/paper'

export default {
  name: 'answer',
  props: {
    id: {
      type: Number,
    },
  },
  components: {

  },
  data() {
    return {
      questionList:[],
      currentIndex:0,
      currentModel:{},
      currentPage:''
    }
  },
  computed: {},
  watch: {},
  methods: {
    getList() {
      getPaper({id: this.id}).then(response => {
        this.questionList = response.questions
        this.updateData()
      }).catch(error => {
        console.log(error);
      })
    },
    updateData() {
      for (let index = 0; index < this.questionList.length; index++) {
        let element = this.questionList[index];
        let array = element.optional.split(',')
        this.$set(element, 'optionArray', array)
        this.$set(element, 'radio', '-1')
      }
      
      this.currentIndex = 0
      this.updatePageUI()
    },
    updatePageUI() {
      if (this.currentIndex < this.questionList.length) {
        this.currentModel = this.questionList[this.currentIndex]
        this.currentPage = (this.currentIndex + 1) + '/' + this.questionList.length
      }
    },
    changeBtn(type) {
      if (type === 0) { //上一提
        this.currentIndex --
      } else {
        this.currentIndex ++
      }
      this.updatePageUI()
    },
    submitAnswer() {
      for (let index = 0; index < this.questionList.length; index++) {
        const element = this.questionList[index];
        if (element.radio === '-1') {
          this.$message.error('请回答所有题目')
          return
        }
      }
      let answer = this.questionList.map(item => {
        return item.radio
      })

      console.log(111, answer.toString());
      saveAnswer({ answers: answer.toString(), personId: this.id }).then(response => { 
        this.$message.success('提交成功')
      this.$emit('handleSubmit')
      }).catch(error => {
        console.log(error);
      })
    }
  },
  created() {
    this.getList()
  },
  mounted() {
  },
  beforeCreate() {},
  beforeMount() {},
  beforeUpdate() {},
  updated() {},
  beforeDestroy() {},
  destroyed() {},
  activated() {},
}
</script>

<style lang='scss' scoped>
  .page {
    position: relative;
    width: 100%;
    height: 100%;

    .smallLogo {
      @include flex-center;
      margin-top: 30px;
      width: 100%;
      height: 60px;
      background: white;
      padding: 2px;
      img {
        height: 100%;
      }
    }

    .title1 {
      margin-top: 30px;
      width: 100%;
      font-size: 20px;
      font-weight: bold;
      text-align: center;
      color: rgb(3, 117, 97);
    }

    .title2 {
      width: 100%;
      font-size: 26px;
      font-weight: bold;
      text-align: center;
      color: rgb(3, 117, 97);
    }

    .wrapper {
      position: absolute;
      top: 150px;
      bottom: 200px;
      left: 0;
      right: 0;
      width: 100%;
      display: flex;
      align-items: center;
      
      .content {
        margin-left: 15px;
        margin-right: 10px;
        .title {
          font-size: 23px;
        }
        .option {
          .el-radio {
            margin-top: 25px;
            margin-left: 15px;
            display: block;
          }

          .el-radio ::v-deep .el-radio__label{
            font-size:20px !important;
          }
        }
      }
    }

    .buttonBg {
      position: absolute;
      bottom: 0;
      width: 100%;
      height: 200px;

      .info {
        position: absolute;
        top: 5px;
        left: 50%;
        width: 45px;
        transform: translateX(-50%);
        font-size: 15px;
      }

      .leftBtn {
        position: absolute;
        left: 35px;
      }

      .rightBtn {
        position: absolute;
        right: 35px;
      }
    }
  }
</style>